package com.cy.service.impl;

import com.cy.common.ResultResponse;
import com.cy.entity.Courses;
import com.cy.entity.Usercourses;
import com.cy.entity.vo.ChooseCourseVo;
import com.cy.mapper.UsercoursesMapper;
import com.cy.service.IUsercoursesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Aleven
 * @since 2024-12-21
 */
@Service
public class UsercoursesServiceImpl extends ServiceImpl<UsercoursesMapper, Usercourses> implements IUsercoursesService {
    @Autowired
    private UsercoursesMapper usercoursesMapper;

    @Override
    public ResultResponse chooseCourse(Integer userId,Integer courseID) throws Exception {
        if (userId==null||courseID==null){
            return ResultResponse.error();
        }

        Integer i = usercoursesMapper.ifExist(userId,courseID);
        if (i!=null){
            return ResultResponse.exist();
        }
        Usercourses u = new Usercourses();
        u.setCourseID(courseID);
        u.setCompletionStatus("进行中");
        u.setUserID(userId);
        Integer count = usercoursesMapper.addUserCourses(u);
        return count>0?ResultResponse.success():ResultResponse.fail();
    }

    @Override
    public ResultResponse selectCourseRecordByUserId(Integer userId) throws Exception {
        if (userId==null){
            return ResultResponse.error();
        }
        List<Usercourses> usercoursesList = usercoursesMapper.selectCourseRecordByUserId(userId);
        return ResultResponse.success(usercoursesList);
    }

    @Override
    public ResultResponse exitCourse(Integer UserCourseID) throws Exception {
        if (UserCourseID == null ) {
            return ResultResponse.error();
        }

        Integer count = usercoursesMapper.exitCourse(UserCourseID);
        return count > 0 ? ResultResponse.success() : ResultResponse.fail();
    }

    @Override
    public Integer countUsercourses(String CourseID) {
        return usercoursesMapper.countUsercourses(CourseID);
    }

    //根据用户兴趣推荐课程
    @Override
    public List<Courses> RecommendByInterest(Integer UserID){
        return usercoursesMapper.RecommendByInterest(UserID);
    }


}
